package com.zhuhjay.system.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * redis配置文件
 * @author ZhuHJay
 * @date 2022/3/30 18:57
 */
@Configuration
public class RedisConfiguration {

    /** 注入自定义的RedisTemplate, 默认的序列化可能不尽人意 **/
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory){
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        /* 把配置文件的配置设置进去, 不然使用不了 */
        redisTemplate.setConnectionFactory(factory);
        /* 指定键的序列化: 字符串格式 */
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        /* 指定值的序列化: json格式 */
        redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(Object.class));
        return redisTemplate;
    }

}
